package com.example.dbsample.config.multithread;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * 多线程JOB
 * 加上该注解前，是严格按顺序执行
 * 加上该注解后，是先执行父类线程，再支持该方法
 */
@Configuration
@EnableAsync
public class AsyncConfig {
    @Value("${sample.core.async.thread.core.size:10}")
    private int threadPoolCount;

    @Value("${sample.thread.pool.await.termination.seconds:30}")
    private int awaitTerminationSeconds;

    public static final String TASK_BEAN = "taskExecutor";

    public static final String LUTEL_BEAN = "lutelExecutor";

    public static final String DROP_BEAN = "dropUserExecutor";

    public static final String IVR_BEAN = "ivrExecutor";

    public static final String BATCH_RECORD_FILE = "batchRecordFileExecutor";

    /**
     * Spring通用线程池，bean名称taskExecutor
     * 标识注解时，指定线程池的bean名称 @Async("taskExecutor")
     */
    @Bean(TASK_BEAN)
    public Executor taskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        // 核心线程数
        executor.setCorePoolSize(threadPoolCount);
        // 最大线程数
        executor.setMaxPoolSize(threadPoolCount * 2);
        // 线程池队列深度
        executor.setQueueCapacity(threadPoolCount * 10);
        // 线程池名前缀
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        // 日志打印时便于标识线程池归属
        executor.setThreadNamePrefix("async-thread");

        // 补充一些参数
        // 线程池活跃时间
        executor.setKeepAliveSeconds(60);
        // 线程池拒绝策略
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        // 等线程都完成工作后关闭线程池
        executor.setWaitForTasksToCompleteOnShutdown(true);
        // 设置线程池中任务的等待世界，如果超过这个时候还没有销毁就强制销毁
        executor.setAwaitTerminationSeconds(awaitTerminationSeconds);
        return executor;
    }


    /**
     * Spring通用线程池，bean名称taskExecutor
     * 标识注解时，指定线程池的bean名称 @Async("dropUserExecutor")
     */
    @Bean(DROP_BEAN)
    public Executor dropExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(threadPoolCount);
        executor.setMaxPoolSize(threadPoolCount * 2);
        executor.setQueueCapacity(threadPoolCount * 10);
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        // 日志打印时便于标识线程池归属
        executor.setThreadNamePrefix("drop-thread");
        executor.setKeepAliveSeconds(60);
        executor.setWaitForTasksToCompleteOnShutdown(true);
        executor.initialize();
        return executor;
    }


    /**
     * Spring通用线程池，bean名称taskExecutor
     * 标识注解时，指定线程池的bean名称 @Async("dropUserExecutor")
     */
    @Bean(IVR_BEAN)
    public Executor ivrExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(threadPoolCount);
        executor.setMaxPoolSize(threadPoolCount * 2);
        executor.setQueueCapacity(threadPoolCount * 10);
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        // 日志打印时便于标识线程池归属
        executor.setThreadNamePrefix("ivr-thread");
        executor.initialize();
        return executor;
    }
}
